package com.anzia.list;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

/**
 *@ClassName ArrayList_Demo
 *@Description
 *@Author Anzia.
 *@Date 2021/4/10 10:52
 *@Version 1.0
 **/
public class ArrayList_Demo {
    public static void main(String[] args) {
        //创建集合
        ArrayList<Object> list = new ArrayList<>();
        Student student1 = new Student("周星驰",20);
        Student student2 = new Student("刘德华",18);
        Student student3 = new Student("梁朝伟",19);

        //添加数据
        list.add(student1);
        list.add(student2);
        list.add(student3);
        System.out.println(list.size());
        System.out.println(list.toString());

        //删除元素
        //list.remove(new Student("周星驰",20));
        //System.out.println(list.toString());

        //遍历
        //使用迭代器
        System.out.println("---------");
        Iterator<Object> iterator = list.iterator();
        while (iterator.hasNext()) {
            Object next = iterator.next();
            System.out.println(next);
        }
        //使用列表迭代器
        System.out.println("---------");
        ListIterator<Object> listIterator = list.listIterator();
        while (listIterator.hasNext()) {
            Object next = listIterator.next();
            System.out.println(listIterator.nextIndex() + " : " + next);
        }
        System.out.println("---------");
        while (listIterator.hasPrevious()) {
            Object previous = listIterator.previous();
            System.out.println(listIterator.nextIndex() + " : " + previous);
        }

        //判断
        System.out.println(list.contains(new Student("周星驰", 20)));
        System.out.println(list.isEmpty());

        //查找
        System.out.println(list.indexOf(new Student("周星驰", 20)));
    }
}
